package com.power.structure.hash;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/15 1:43 下午
 */
public class HashTable {
    private int CAPACITY = 7;
    Node[] data;

    public HashTable() {
        data = new Node[CAPACITY];
    }

    class Node {
        Node next;
        int value;

        public Node(int value) {
            this.value = value;
        }
    }

    public int hash(Node node) {
        return node.value * 31 % 7;
    }

    public void add(int value) {
        Node newNode = new Node(value);
        int location = hash(newNode);

        if (data[location] == null) {
            data[location] = newNode;
        } else {
            Node tmp = data[location];
            while (tmp.next != null) {
                tmp = tmp.next;
            }
            tmp.next = newNode;
        }

    }

    public void display() {
        for (int i = 0; i < data.length; i++) {
            Node node = data[i];

            while (node != null) {
                System.out.print("   "+node.value);
                node = node.next;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        HashTable hashTable = new HashTable();
        for (int i = 0; i < 50; i++) {
            hashTable.add(i);
        }
        hashTable.display();
    }


}
